/**
 * @(#)YcsDateUtils.java, 2021/12/21.
 * <p/>
 * Copyright 2021 Netease, Inc. All rights reserved.
 * NETEASE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package site.ycsyxt.common.util;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.util.Assert;

/**
 * 时间操作相关工具类
 *
 * @author ycs(15241496739 @ 163.com)
 * @date 2021/12/26
 **/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYYMMDD = "yyyyMMdd";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前日期字符串, 格式为yyyy-MM-dd
     *
     * @return
     */
    public static String getNowDate() {
        return parseDateToStr(YYYY_MM_DD, new Date());
    }

    /**
     * 获取当前时间字符串, 格式为yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getNowTime() {
        return parseDateToStr(YYYY_MM_DD_HH_MM_SS, new Date());
    }

    /**
     * 获取日期字符串, 格式为yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getDate(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    /**
     * 获取时间字符串, 格式为yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String getTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    /**
     * 将日期解析成特定格式的字符串
     *
     * @param format
     * @param date
     * @return
     */
    private static String parseDateToStr(final String format, final Date date) {
        return DateFormatUtils.format(date, format);
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static long currentTime() {
        return System.currentTimeMillis();
    }

    /**
     * 将时间戳解析成特定格式的字符串
     *
     * @param timeStamp
     * @return
     */
    public static String parseTimeStampToStr(final long timeStamp) {
        return DateFormatUtils.format(timeStamp, YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 日期路径,年月日 如20180808
     *
     * @param date
     * @return
     */
    public static String getYyyyMmDd(final Date date) {
        return DateFormatUtils.format(date, YYYYMMDD);
    }

    /**
     * 日期型字符串转化为日期
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String str) {
        Assert.isTrue(str != null, "参数错误！");
        try {
            return parseDate(str, parsePatterns);
        } catch (ParseException e) {
            throw new RuntimeException("无法将字符串解析成日期, str = " + str + "\n" + e);
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
}
